package dao;

import common.User;
import utils.MD5;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class checklogin {
    public boolean checkLogin(User users, String password2) {
        System.out.println(users.getId());
        Connection conn=null;
        ResultSet res=null;
        String psw;
        MD5 md5 = new MD5();
        try {
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            //注册驱动
            Class.forName(Driver);
            //获取连接
            conn= DriverManager.getConnection(url,user,password);
            String sql="select * from users where userid = ?";
            PreparedStatement presta = conn.prepareStatement(sql);
            presta.setString(1,users.getId() );
            res=presta.executeQuery();
            if(res.next()){
                users.setPsw(res.getString("password"));
                if(users.getPsw().equals(md5.generateMD5(password2))){
                    return true;
                }else {
                    return false;
                }
            }
        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
}
